package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.aop.annotation.LogAnnotation;
import com.company.project.common.utils.Func;
import com.company.project.entity.*;
import com.company.project.mapper.SysDeptMapper;
import com.company.project.mapper.TGzszMapper;
import com.company.project.mapper.TSeatsMapper;
import com.company.project.mapper.TWdkhMapper;
import com.company.project.service.HttpSessionService;
import com.company.project.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.service.TGjjlService;

import javax.annotation.Resource;


/**
 * 
 *
 * @author wenbin
 * @email *****@mail.com
 * @date 2023-12-23 16:46:20
 */
@Controller
@RequestMapping("/")
public class TGjjlController {
    @Resource
    private TGjjlService tGjjlService;
    @Resource
    private TWdkhMapper wdkhMapper;
    @Resource
    private UserService userService;
    @Resource
    private TSeatsMapper seatsMapper;
    @Resource
    private TGzszMapper gzszMapper;
    /**
    * 跳转到页面
    */
    @GetMapping("/index/tGjjl/{wdkhId}")
    public String tGjjl(Model model, @PathVariable("wdkhId") String wdkhId) {
        model.addAttribute("wdkhId",wdkhId);
        return "tgjjl/list";
        }

    @GetMapping("/index/tGjjl/add/{wdkhId}")
    public String tGjjladd(Model model, @PathVariable("wdkhId") String wdkhId) {
        model.addAttribute("wdkhId",wdkhId);
        return "tgjjl/add";
    }

    @ApiOperation(value = "新增")
    @PostMapping("tGjjl/add")
    @LogAnnotation(title = "跟进记录", action = "新增")
//    @RequiresPermissions("tGjjl:add")
    @ResponseBody
    public DataResult add(@RequestBody TGjjlEntity tGjjl){
        SysUser userInfo = userService.getUserInfo();
        TSeatsEntity tSeatsEntity = seatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery()
                .eq(TSeatsEntity::getAccountid, userInfo.getId()));
        //根据公司名称查询规则
        TGzszEntity tGzszEntity = gzszMapper.selectOne(Wrappers.<TGzszEntity>lambdaQuery().eq(TGzszEntity::getSsgs, tSeatsEntity.getCorporatename()));
        TWdkhEntity tWdkhEntity = wdkhMapper.selectById(tGjjl.getWdkhid());
        tGjjl.setXs(tWdkhEntity.getSsxs());
        //跟进记录  更新下次更新时间
        if (Func.isNotEmpty(tGjjl.getXclxtime())){
            Integer wcjhl = tGzszEntity.getWcjhl();//未成交回流
            Integer wgjhl = tGzszEntity.getWgjhl();//未跟进回流
            Date lrgh2 = tWdkhEntity.getRktime();
            if (wcjhl > 0 && wgjhl > 0){
                //设置流入公海时间  未成交回流   入库时间加上设置时间
                Calendar calendar2 = Calendar.getInstance();
                calendar2.setTime(lrgh2);
                //加上规定时间
                calendar2.add(Calendar.DAY_OF_MONTH, wcjhl);
                lrgh2 = calendar2.getTime();
                Date lrgh = new Date();

                //设置流入公海时间  未跟进回流
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(lrgh);
                //加上规定时间
                calendar.add(Calendar.DAY_OF_MONTH, wgjhl);
                lrgh = calendar.getTime();
                Date hlDay = lrgh.compareTo(lrgh2) > 0 ? lrgh2 : lrgh;

                tWdkhEntity.setLrghtime(hlDay);
            }
            tWdkhEntity.setXclxtime(tGjjl.getXclxtime());
        }
        tWdkhEntity.setZhgjtime(new Date());
        tWdkhEntity.setKhjb(tGjjl.getKhjb());//意向度
        wdkhMapper.updateById(tWdkhEntity);

        tGjjlService.save(tGjjl);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("tGjjl/delete")
    @LogAnnotation(title = "跟进记录", action = "删除")
    @RequiresPermissions("tGjjl:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        tGjjlService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @LogAnnotation(title = "跟进记录", action = "更新")
    @PutMapping("tGjjl/update")
//    @RequiresPermissions("tGjjl:update")
    @ResponseBody
    public DataResult update(@RequestBody TGjjlEntity tGjjl){
        tGjjlService.updateById(tGjjl);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @LogAnnotation(title = "跟进记录", action = "查询分页数据")
    @PostMapping("tGjjl/listByPage")
//    @RequiresPermissions("tGjjl:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody TGjjlEntity tGjjl){
        Page page = new Page(tGjjl.getPage(), tGjjl.getLimit());
        LambdaQueryWrapper<TGjjlEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        queryWrapper.eq(TGjjlEntity::getWdkhid, tGjjl.getWdkhid());
        queryWrapper.orderByDesc(TGjjlEntity::getAddtime);
        IPage<TGjjlEntity> iPage = tGjjlService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
